<template>
  <div class="tactInfo-testExplainDescript">
    <el-form :model="ruleForm" :inline="true" ref="formRef" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item
            label="弹目距离"
            prop="bulletTargetDistance"
            :rules="{
              required: true,
              message: '请选择',
              trigger: 'blur',
            }"
          >
            <el-input
              v-model="ruleForm.bulletTargetDistance"
              placeholder="请输入"
              :disabled="disabled"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <TagIcon title="背景环境过程描述项" />
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item
            label="海况等级"
            prop="waveLevel"
            :rules="{
              required: true,
              message: '请输入',
              trigger: 'blur',
            }"
          >
            <el-input
              v-model="ruleForm.waveLevel"
              placeholder="请输入"
              :disabled="disabled"
            />
          </el-form-item>
          <el-form-item label="其他" prop="elseInfo">
            <el-input
              v-model="ruleForm.elseInfo"
              type="textarea"
              :rows="4"
              placeholder="请输入"
              :disabled="disabled"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="风速" prop="windSpeed">
            <el-input
              v-model="ruleForm.windSpeed"
              placeholder="请输入"
              :disabled="disabled"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="风向" prop="windDirection">
            <el-input
              v-model="ruleForm.windDirection"
              placeholder="请输入"
              :disabled="disabled"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <div class="form-footer common-buttons" v-if="!disabled">
        <el-button type="primary" @click="onSubmit(formRef, 'form')"
          >保存</el-button
        >
        <el-button type="primary" @click="onReset(formRef)">取消</el-button>
      </div>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, watch } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { httpSaveTactInfoExplainDescriptItem } from '@/api/testSampleData';
import { useRoute, useRouter } from 'vue-router';
import TagIcon from '@/components/tag-icon/index.vue';
const route = useRoute();

const isLook = route.query.layerType === 'look' ? true : false;
const disabled = ref<boolean>(isLook);

const formRef = ref<FormInstance>();
const validateState4 = ref(false);
const props = defineProps({
  dataSourceObject: {
    type: Object,
    default: () => {
      return {};
    },
  },
});
// 背景环境过程描述项 bgEnviProcessInfoVO
const ruleForm = ref({
  bulletTargetDistance: '',
  waveLevel: '',
  windSpeed: '',
  windDirection: '',
  elseInfo: '',
});

watch(
  () => props.dataSourceObject,
  (newValue, oldValue) => {
    const { dynamicPara } = newValue;
    if (dynamicPara && dynamicPara.length) {
      const { bgEnviProcessInfoVO, bulletTargetDistance } = dynamicPara[0];
      ruleForm.value = { ...bgEnviProcessInfoVO, bulletTargetDistance };
    }
  }
);

// 保存
const onSubmit = async (
  formEl: FormInstance | undefined,
  submitType: string
) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      validateState4.value = false;
      const params = {
        bgEnviParamList: [{ ...ruleForm.value }],
        sampleId: route.query.sampleId,
      };
      httpSaveTactInfoExplainDescriptItem(params).then((res: any) => {
        const { code, message } = res;
        if (code === 200) {
          // 当submitType为form，表单提交
          if (submitType === 'form') {
            ElMessage({
              message: message || '保存成功',
              type: 'success',
            });
          }
        }
      });
    } else {
      validateState4.value = true;
      console.log('error submit!', fields);
    }
  });
};

const onReset = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

defineExpose({ onSubmit, formRef, validateState4 });
</script>

<style lang="scss" scoped>
.tactInfo-testExplainDescript {
  padding: 20px;
  height: calc(100vh - 290px);
  // width: 100%;
  overflow-y: scroll;
  .el-form--inline .el-form-item {
    width: 100%;
  }
  .el-select--small {
    width: 100%;
  }
  .el-row {
    margin-right: 0 !important;
  }
  .form-footer {
    display: flex;
    justify-content: center;
    margin-top: 30px;
  }
}
</style>
